<?php
if(!defined('IN_WOGU')) {
    exit('Access Denied');
}

class Session_Mysql {
    protected $_db;

    public function init($config) {
        $this->_db = Loader::loadClass($config['model'], 1);
        session_set_save_handler(array(&$this, 'open'), array(&$this, 'close'), array(&$this, 'read'), array(&$this, 'write'), array(&$this, 'destroy'), array(&$this, 'gc'));
        session_start();
    }

    public function open($save_path, $session_name) {
        return true;
    }

    public function close() {
        return true;
    }

    public function read($id) {
        return $this->_db->read($id);
    }

    public function write($id, $data) {
        return $this->_db->write($id, $data);
    }

    public function destroy($id) {
        return $this->_db->deleteSession($id);
    }

    public function gc($maxlifetime) {
        $expiretime = SYS_TIME - $maxlifetime;
        return $this->_db->deleteExpired($expiretime);
    }
}